#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->pushButton->setVisible(false);
    ui->pushButton_2->setVisible(false);
    ui->input1->setVisible(false);
    ui->input2->setVisible(false);
    ui->pushButton_3->setVisible(false);
    ui->pushButton_4->setVisible(false);
    ui->label->setVisible(false);
    ui->label_2->setVisible(false);
    ui->info2->setVisible(false);
    ui->info1->setVisible(false);
    ui->pushButton_5->setVisible(false);
    ui->modebtn->setVisible(false);
    ui->show3->setVisible(false);
    ui->pushButton6->setVisible(false);
    mode=false;
    single=true;
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    show1.show();
}

void MainWindow::on_pushButton_3_clicked()
{
    show1.setColor1(ui->input1->text().toInt());
    ui->info1->setText(tr("C: ")+ui->input1->text());
}

void MainWindow::on_pushButton_4_clicked()
{
    show1.setColor1(ui->input1->text().toInt());
    show1.setColor2(ui->input2->text().toInt());
    ui->info1->setText(tr("C1:")+ui->input1->text()+tr("  C2:")+ui->input2->text());
}

void MainWindow::on_pushButton_5_clicked()
{
    show1.savedata("data.png");
}

void MainWindow::on_pushButton_2_clicked()
{
    show1.reloadData("data.png");
}

void MainWindow::on_start3_clicked()
{
    QString name = ui->start2->text();
    QByteArray tstr = name.toLocal8Bit();
    char *data = tstr.data();
    FILE *p=fopen(data,"r");
    if(p==NULL)
    {
        ui->start1->setText(tr("file is not exist!"));
        return ;
    }
    fclose(p);

    show1.init(data);
    ui->start1->setVisible(false);
    ui->start2->setVisible(false);
    ui->start3->setVisible(false);

    ui->pushButton->setVisible(true);
    ui->pushButton_2->setVisible(true);
    ui->input1->setVisible(true);
    ui->input2->setVisible(true);
    ui->pushButton_3->setVisible(true);
    ui->pushButton_4->setVisible(true);
    ui->label->setVisible(true);
    ui->label_2->setVisible(true);
    ui->info2->setVisible(true);
    ui->info1->setVisible(true);
    ui->pushButton_5->setVisible(true);
    ui->pushButton6->setVisible(true);
    ui->modebtn->setVisible(true);
    ui->show3->setVisible(true);
}

void MainWindow::on_modebtn_clicked()
{
    if(mode)
        mode=false;
    else
        mode=true;

    if(mode)
        ui->modebtn->setText(tr("mode1"));
    else
        ui->modebtn->setText(tr("mode0"));

    show1.setMode(mode);
}

void MainWindow::on_start2_returnPressed()
{
    on_start3_clicked();
}

void MainWindow::on_show3_clicked()
{
    QString name = ui->start2->text();
    QByteArray tstr = name.toLocal8Bit();
    char *data = tstr.data();
    FILE *p=fopen(data,"r");
    if(p==NULL)
    {
        ui->start1->setText(tr("file is not exist!"));
        return ;
    }
    fclose(p);

    showpic.init(data,"data.png");
    showpic.show();
}

void MainWindow::on_input1_returnPressed()
{
    show1.setColor1(ui->input1->text().toInt());
    show1.setColor2(ui->input2->text().toInt());
    ui->info1->setText(tr("C1:")+ui->input1->text()+tr("  C2:")+ui->input2->text());
}

void MainWindow::on_input2_returnPressed()
{
    show1.setColor1(ui->input1->text().toInt());
    show1.setColor2(ui->input2->text().toInt());
    ui->info1->setText(tr("C1:")+ui->input1->text()+tr("  C2:")+ui->input2->text());
}

void MainWindow::on_input1_textChanged(const QString &arg1)
{
    on_input1_returnPressed();
}

void MainWindow::on_input2_textChanged(const QString &arg1)
{
    on_input2_returnPressed();
}

void MainWindow::on_modebtn2_clicked()
{
    if(single)
        single=false;
    else
        single=true;

    if(single)
        ui->modebtn->setText(tr("Single"));
    else
        ui->modebtn->setText(tr("Multi"));

    show1.setDrawLineMode(single);
}

void MainWindow::on_pushButton6_clicked()
{
    show1.reloadData("temp.png");
}




